﻿
using Dapper;
using System;
using BCEntity.MySql.BusinessFlowStepInstance;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.MySql.BusinessFlowStepInstance
{
    /// <summary>
    /// OA系统业务流步骤实例
    /// </summary>
    public class BusinessFlowStepInstanceData : IBusinessFlowStepInstanceData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_BusinessFlowStepInstance";
        private const string QUERY = @"SELECT BusinessFlowStepInstanceId,PageBusinessConfigId,PageBusinessCode,PageBusinessName,CreateTime,BusinessFlowStepInstanceName,BusinessFlowStepModelCode,
                                            BusinessFlowInstanceId,BusinessFlowInstanceName,BusinessFlowModelId,BusinessFlowModelName,BusinessFlowStepModelId,BusinessFlowStepModelName,
                                            BusinessFlowModelCode,Step
                                        FROM OA_BusinessFlowStepInstance";
        public BusinessFlowStepInstanceData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long businessFlowStepInstanceId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowStepInstanceId=?BusinessFlowStepInstanceId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepInstanceId", businessFlowStepInstanceId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Add(List<BusinessFlowStepInstanceEntity> entities)
        {
            List<string> sqls = new List<string>();
            List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();
            string sql = SqlCommands.GetInsertSql("OA_BusinessFlowStepInstance", new string[] { "PageBusinessConfigId", "PageBusinessCode", "PageBusinessName", "BusinessFlowStepInstanceName",
                    "BusinessFlowInstanceId", "BusinessFlowInstanceName","BusinessFlowModelId", "BusinessFlowModelName", "BusinessFlowModelCode","BusinessFlowStepModelId",
                    "BusinessFlowStepModelName","BusinessFlowStepModelCode", "Step" });
            foreach (var entity in entities)
            {
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64, size: 20);
                parameters.Add("?PageBusinessCode", entity.PageBusinessCode?.Trim(), DbType.AnsiString, size: 150);
                parameters.Add("?PageBusinessName", entity.PageBusinessName?.Trim(), DbType.AnsiString, size: 50);
                parameters.Add("?BusinessFlowStepInstanceName", entity.BusinessFlowStepInstanceName?.Trim(), DbType.AnsiString, size: 100);
                parameters.Add("?BusinessFlowInstanceId", entity.BusinessFlowInstanceId, DbType.Int64, size: 20);
                parameters.Add("?BusinessFlowInstanceName", entity.BusinessFlowInstanceName?.Trim(), DbType.AnsiString, size: 50);
                parameters.Add("?BusinessFlowModelId", entity.BusinessFlowModelId, DbType.Int64, size: 20);
                parameters.Add("?BusinessFlowModelName", entity.BusinessFlowModelName?.Trim(), DbType.AnsiString, size: 20);
                parameters.Add("?BusinessFlowModelCode", entity.BusinessFlowModelCode?.Trim(), DbType.AnsiString, size: 50);
                parameters.Add("?BusinessFlowStepModelId", entity.BusinessFlowStepModelId, DbType.Int64, size: 20);
                parameters.Add("?BusinessFlowStepModelName", entity.BusinessFlowStepModelName?.Trim(), DbType.AnsiString, size: 20);
                parameters.Add("?BusinessFlowStepModelCode", entity.BusinessFlowStepModelCode?.Trim(), DbType.AnsiString, size: 50);
                parameters.Add("?Step", entity.Step, DbType.Int32, size: 11);
                sqls.Add(sql);
                dynamicParameters.Add(parameters);
            }
            return await this.databaseContext.Master.TransactionExecuteAsync(sqls, dynamicParameters, databaseContext.Transaction) > 0;

        }

        public async Task<BusinessFlowStepInstanceEntity> Update(BusinessFlowStepInstanceEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_BusinessFlowStepInstance", new string[] { "PageBusinessConfigId", "PageBusinessCode", "PageBusinessName", "BusinessFlowStepInstanceName",
                "BusinessFlowInstanceId", "BusinessFlowInstanceName","BusinessFlowModelCode", "BusinessFlowModelId", "BusinessFlowModelName", "BusinessFlowStepModelId", "BusinessFlowStepModelName",
                "BusinessFlowStepModelCode","Step" }, new string[] { "BusinessFlowStepInstanceId" });
            sql = string.Format("{0} {1} WHERE BusinessFlowStepInstanceId=?BusinessFlowStepInstanceId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepInstanceId", entity.BusinessFlowStepInstanceId, DbType.Int64, size: 20);
            parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64, size: 20);
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode?.Trim(), DbType.AnsiString, size: 150);
            parameters.Add("?PageBusinessName", entity.PageBusinessName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?BusinessFlowStepInstanceName", entity.BusinessFlowStepInstanceName?.Trim(), DbType.AnsiString, size: 100);
            parameters.Add("?BusinessFlowInstanceId", entity.BusinessFlowInstanceId, DbType.Int64, size: 20);
            parameters.Add("?BusinessFlowInstanceName", entity.BusinessFlowInstanceName?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?BusinessFlowModelId", entity.BusinessFlowModelId, DbType.Int64, size: 20);
            parameters.Add("?BusinessFlowModelName", entity.BusinessFlowModelName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?BusinessFlowModelCode", entity.BusinessFlowModelCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?BusinessFlowStepModelId", entity.BusinessFlowStepModelId, DbType.Int64, size: 20);
            parameters.Add("?BusinessFlowStepModelName", entity.BusinessFlowStepModelName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?BusinessFlowStepModelCode", entity.BusinessFlowStepModelCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?Step", entity.Step, DbType.Int32, size: 11);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<BusinessFlowStepInstanceEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long businessFlowStepInstanceId)
        {
            string sql = @"DELETE FROM OA_BusinessFlowStepInstance WHERE BusinessFlowStepInstanceId=?BusinessFlowStepInstanceId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepInstanceId", businessFlowStepInstanceId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<BusinessFlowStepInstanceEntity> GetEntity(long businessFlowStepInstanceId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowStepInstanceId=?BusinessFlowStepInstanceId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowStepInstanceId", businessFlowStepInstanceId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<BusinessFlowStepInstanceEntity>(sql, parameters);
        }

        public async Task<IEnumerable<BusinessFlowStepInstanceEntity>> GetList(long businessFlowInstanceId)
        {
            string sql = string.Format(@"{0} WHERE BusinessFlowInstanceId=?BusinessFlowInstanceId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BusinessFlowInstanceId", businessFlowInstanceId, DbType.Int64);
            return await this.databaseContext.Slave.QueryAsync<BusinessFlowStepInstanceEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<BusinessFlowStepInstanceEntity>, int)> Query(long? businessFlowModelId, long? businessFlowInstanceId, long? pageBusinessConfigId,string businessFlowStepInstanceName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (businessFlowModelId.HasValue)
            {
                wheres.Add("BusinessFlowModelId=?BusinessFlowModelId");
                parameters.Add("?BusinessFlowModelId", businessFlowModelId, DbType.Int64, size: 20);
            }
            if (businessFlowInstanceId.HasValue)
            {
                wheres.Add("BusinessFlowInstanceId=?BusinessFlowInstanceId");
                parameters.Add("?BusinessFlowInstanceId", businessFlowInstanceId, DbType.Int64, size: 20);
            }
            if (pageBusinessConfigId.HasValue)
            {
                wheres.Add("PageBusinessConfigId=?PageBusinessConfigId");
                parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64, size: 20);
            }
            if (!string.IsNullOrWhiteSpace(businessFlowStepInstanceName))
            {
                wheres.Add("BusinessFlowStepInstanceName LIKE IN CONCAT('%',CONCAT(?BusinessFlowStepInstanceName,'%'))");
                parameters.Add("?BusinessFlowStepInstanceName", businessFlowStepInstanceName?.Trim(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_BusinessFlowStepInstance {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<BusinessFlowStepInstanceEntity>(sql, parameters);
        }
    }
}
